package com.ruoyi.system.service.impl;



import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.HgtOperationDo;
import com.ruoyi.system.domain.HgtSumDo;
import com.ruoyi.system.domain.HgtpieDo;
import com.ruoyi.system.domain.MyCost;
import com.ruoyi.system.mapper.HgtOperationMapper;
import com.ruoyi.system.service.HgtOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;


@Service
public class HgtOperationServiceImpl implements HgtOperationService {
	@Autowired
	private HgtOperationMapper hgtOperationMapper;
	@Autowired
	protected Validator validator;

	@Override
	public void saveHgtOperation(HgtOperationDo hgtOperationDo) {
		hgtOperationMapper.saveHgtOperation(hgtOperationDo);

	}

	@Override
	public void updateHgtOperation(HgtOperationDo hgtOperationDo) {
		hgtOperationMapper.updateHgtOperation(hgtOperationDo);
	}

	@Override
	public HgtSumDo getSumHgtOperation() {
		return hgtOperationMapper.getSumHgtOperation();
	}

	@Override
	public int findUser(String name, String password) {
		return hgtOperationMapper.findUser(name,password);
	}

	@Override
	public List<HgtOperationDo> findHgtOperation(String creatorname) {
		return hgtOperationMapper.findHgtOperation(creatorname);
	}

	@Override
	public List<HgtOperationDo> findHgtOperationsearch(String creatorname, String search) {
		return hgtOperationMapper.findHgtOperationsearch(creatorname,search);
	}

	@Override
	public List<HgtOperationDo> findHgtOperationall() {
		return hgtOperationMapper.findHgtOperationall();
	}

	@Override
	public List<HgtpieDo> findSumHgtOperation() {
		return hgtOperationMapper.findSumHgtOperation();
	}

	@Override
	public List<HgtpieDo> findSumHgtOperation2() {
		return  hgtOperationMapper.findSumHgtOperation2();
	}

	@Override
	public String importCost(List<HgtOperationDo> myHgtOperationlist) {
		if (StringUtils.isNull(myHgtOperationlist) || myHgtOperationlist.size() == 0)
		{
			throw new ServiceException("导入数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (HgtOperationDo myHgtOperation : myHgtOperationlist)
		{
			try
			{
				// 验证是否存在这个用户
				HgtOperationDo u = hgtOperationMapper.selectHgtOperationById(myHgtOperation.getId());
				if (StringUtils.isNull(u))
				{
					BeanValidators.validateWithException(validator, myHgtOperation);
					hgtOperationMapper.saveHgtOperation(myHgtOperation);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + myHgtOperation.getId()+ " 导入成功");
				}
				else
				{
					BeanValidators.validateWithException(validator, myHgtOperation);

					hgtOperationMapper.updateHgtOperation(myHgtOperation);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + myHgtOperation.getId() + " 更新成功");
				}
                /*else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }*/
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、账号 " + myHgtOperation.getId() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());

			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	@Override
	public String getuserfindphonerole(String username) {
		return hgtOperationMapper.getuserfindphonerole(username);
	}
}
